package com.youdoneed.community.forum.service;

import com.youdoneed.base.application.AppException;
import com.youdoneed.community.common.utils.StringUUIdUtil;
import com.youdoneed.community.forum.dao.AllowviewmessgaeMapper;
import com.youdoneed.community.forum.entity.Allowviewmessgae;
import com.youdoneed.community.forum.vo.AllowviewmessgaeVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by lid on 2017/12/7 0007.
 */
@Service
public class AllowviewmessageService {
    private final Logger logger = LoggerFactory.getLogger(AllowviewmessageService.class);

    @Autowired
    AllowviewmessgaeMapper allowviewmessgaeMapper;

    @Transactional
    public void insertRecordBatch(String msgId, String msgUserId, List<String> userIds, String msgSource) {
        if (!StringUtils.hasText(msgId)) {
            throw new AppException("msgId为空");
        }

        if (!StringUtils.hasText(msgUserId)) {
            throw new AppException("msgUserId为空");
        }

        if ((null == userIds)
                || (0 == userIds.size())) {
            return;
        }

        List<Allowviewmessgae> listAllowViewMsgs = new ArrayList<>(userIds.size());
        Date createDate = new Date();
        for (String userId : userIds) {
            Allowviewmessgae allowviewmessgae = new Allowviewmessgae();
            allowviewmessgae.setUserId(userId);
            allowviewmessgae.setMsgUserId(msgUserId);
            allowviewmessgae.setMsgId(msgId);
            allowviewmessgae.setCreateDate(createDate);
            allowviewmessgae.setAllowViewMsgId(StringUUIdUtil.getUUID());
            allowviewmessgae.setMsgSource(msgSource);
            listAllowViewMsgs.add(allowviewmessgae);
        }
        logger.info("listAllowViewMsgs.size():" + listAllowViewMsgs.size());
        allowviewmessgaeMapper.insertRecordBatch(listAllowViewMsgs);
        return;
    }

    @Transactional
    public void insert(Allowviewmessgae record) {
        if (null == record) {
            return;
        }

        if ((!StringUtils.hasText(record.getMsgId()))
                || (!StringUtils.hasText(record.getMsgUserId()))
                || (!StringUtils.hasText(record.getUserId()))) {
            throw new AppException("参数为空!");
        }

        if (!StringUtils.hasText(record.getAllowViewMsgId())) {
            record.setAllowViewMsgId(StringUUIdUtil.getUUID());
        }

        if (null == record.getCreateDate()) {
            record.setCreateDate(new Date());
        }

        allowviewmessgaeMapper.insert(record);
    }

    @Async
    @Transactional
    public int deleteByMsgId(String msgId) {
        if (!StringUtils.hasText(msgId)) {
            throw new AppException("msgId为空");
        }

        return allowviewmessgaeMapper.deleteByMsgId(msgId);
    }

    @Async
    @Transactional
    public int deleleByUserIds(String userId, String msgUserId) {
        if (!StringUtils.hasText(userId)) {
            throw new AppException("userId为空");
        }

        if (!StringUtils.hasText(msgUserId)) {
            throw new AppException("msgUserId为空");
        }

        return allowviewmessgaeMapper.deleleByUserIds(userId, msgUserId);
    }

    @Async
    @Transactional
    public int deleleByUserIdAndMsgId(String userId, String msgId) {
        if (!StringUtils.hasText(userId)) {
            throw new AppException("userId为空");
        }

        if (!StringUtils.hasText(msgId)) {
            throw new AppException("msgId为空");
        }

        return allowviewmessgaeMapper.deleleByUserIdAndMsgId(userId, msgId);
    }

    @Transactional
    public List<String> getMsgIdsByUserId(String userId, Date beforeDeadTime) {
        if (!StringUtils.hasText(userId)) {
            throw new AppException("userId为空");
        }

        if (null == beforeDeadTime) {
            beforeDeadTime = new Date();
        }

        return allowviewmessgaeMapper.getMsgIdsByUserId(userId, beforeDeadTime);
    }

    @Transactional
    public List<AllowviewmessgaeVo> getMsgsByUserId(String userId, Date beforeDeadTime) {
        if (!StringUtils.hasText(userId)) {
            throw new AppException("userId为空");
        }

        if (null == beforeDeadTime) {
            beforeDeadTime = new Date();
        }

        return allowviewmessgaeMapper.getMsgsByUserId(userId, beforeDeadTime);
    }

    @Transactional
    public void updateCreateDate(String msgId, Date updateDate){
        if (!StringUtils.hasText(msgId)) {
            throw new AppException("msgId为空");
        }

        if(null == updateDate){
            updateDate = new Date();
        }

        allowviewmessgaeMapper.updateCreateDate(msgId, updateDate);
    }
}
